package artid.com.arttogo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;

import java.util.ArrayList;
import java.util.List;

import artid.com.arttogo.R;
import artid.com.arttogo.adapter.AuctionMyNote_Adapter;
import artid.com.arttogo.bean.AuctionMyNoteParser;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.view.pulllist.PullToRefreshLayout;
import artid.com.arttogo.view.pulllist.PullableListView;

/**
 * 
 *    
 * 项目名称：ArtworkArt   
 * 类名称：AuctionMyNote_Activity   
 * 类描述：我的笔记   
 * 创建人：耿卫斌   
 * 创建时间：2017-4-1 上午9:30:45   
 *
 */
public class AuctionMyNote_Activity extends BaseActivity implements OnClickListener {
	private ImageView searchBtn;
	private PullableListView mListView;
	private PullToRefreshLayout mPullLayout;
	private boolean isRefreshed = false;// 是否刷新
	private int page = 1;
	private LinearLayout noDataLL;
	private AuctionMyNote_Adapter mAdapter;
	private List<AuctionMyNoteParser> list=new ArrayList<AuctionMyNoteParser>();
	private List<AuctionMyNoteParser> more_list=new ArrayList<AuctionMyNoteParser>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_auctionmynote);
		initView();
	}

	private void initView(){
		mContext=AuctionMyNote_Activity.this;
		mBack = (ImageView) findViewById(R.id.back_btn);
		mBack.setOnClickListener(this);
		mTittle = (TextView) findViewById(R.id.tittle_text);
		mTittle.setText("我的笔记");
		searchBtn=(ImageView) findViewById(R.id.AuctionMyNote_title_searchBtn);
		searchBtn.setOnClickListener(this);
		noDataLL = (LinearLayout) findViewById(R.id.ll_hint);
		mListView = (PullableListView) findViewById(R.id.AuctionMyNote_ListView);
		mListView.setOnItemClickListener(onItemClick);
		mPullLayout = (PullToRefreshLayout) findViewById(R.id.AuctionMyNote_refresh_view);
		mPullLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {

			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				// 下拉刷新
				page = 1;
				isRefreshed = true;
				getData();
			}

			@Override
			public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				// 上拉加载
				page++;
				getData();	
			}
		});
		getData();
	}

	@Override
	public void onClick(View v) {
		Intent intent;
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back_btn:
			finish();
			break;
		case R.id.AuctionMyNote_title_searchBtn:
			//搜索
			intent=new Intent(mContext,LotSearchActivity.class);
			startActivity(intent);
			break;

		default:
			break;
		}
	}
	
	OnItemClickListener onItemClick=new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
								long arg3) {
			// TODO Auto-generated method stub
		}
	};

	/**
	 * 获取我的笔记列表
	 */
	private void getData(){
		VolleyBasic.getInstance(this).post(Cache.noteList,
				VolleyMap.getInstance().noteList(page, 10),
				AuctionMyNoteParser.class, new VolleyCallback() {

			@SuppressWarnings({ "unchecked", "static-access" })
			@Override
			public void onSuccess(VolleyData vd) {
				// TODO Auto-generated method stub
				mPullLayout.loadmoreFinish(mPullLayout.SUCCEED);
				list= (List<AuctionMyNoteParser>) vd.getList();
				if (isRefreshed == true) {
					more_list.clear();
					isRefreshed = false;
				}
				if (!list.isEmpty()) {
					more_list.addAll(list);
				}

				//这里是显示有数据和无数据的布局
				if(more_list!=null && more_list.size()!=0){
					noDataLL.setVisibility(View.GONE);
				}else{
					noDataLL.setVisibility(View.VISIBLE);
					return;
				}

				if (mAdapter == null) {
					mAdapter=new AuctionMyNote_Adapter(mContext, more_list);
					mListView.setAdapter(mAdapter);
				}
				mAdapter.notifyDataSetChanged();
			}

			@Override
			public void onError(VolleyError volleyError) {
				// TODO Auto-generated method stub
				mPullLayout.loadmoreFinish(mPullLayout.FAIL);
				//这里是显示有数据和无数据的布局
			/*	if(more_list!=null && more_list.size()!=0){
					noDataLL.setVisibility(View.GONE);
				}else{
					noDataLL.setVisibility(View.VISIBLE);
					return;
				}*/
			}
		});
	}
	
}
